package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.Admin;
import property.pojo.Notice;
import property.pojo.Staff;
import property.service.NoticeService;
import property.service.impl.NoticeServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/Notice/*"})
public class NoticeServlet extends BaseServlet {
    NoticeService noticeService = new NoticeServiceImpl();

    public void addNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character!=2){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Notice notice = JSONObject.toJavaObject(jb.getJSONObject("insert"),Notice.class);
        notice.setnAdminId(((Admin)session.getAttribute("currentUser")).getaId());
        int i = noticeService.addNotice(notice);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "公告发布成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void deleteNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();


        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character!=2){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Integer nId = jb.getInteger("nId");
        int i = noticeService.deleteNotice(nId);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "公告删除成功！");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void updateNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        if(character!=2){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Notice notice = JSONObject.toJavaObject(jb.getJSONObject("update"),Notice.class);
        int i = noticeService.updateNotice(notice);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "新增设施成功，财务已同步更新！");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }
}
